
var start = getDayStr(new Date(new Date().getTime()-24*60*60*1000));
var end = getDayStr(new Date());

function init(){
	var s = params.startTime;
	var e = params.endTime;
	if(s && e){
		start = s;
		end = e;
		con.setIndex(4);
	}else{
		con.setIndex(0);
	}
}

function Controller(){
	var iceChart;
	var legends = {};
	var timeSelector = new TimeSelector();
	this.setIndex = function(index){
		changeTime(index);
		timeSelector.setIndex(index);//添加选择的class
		queryByTime();
	};
	this.query = function(){
		start = $('#start').val();
		end = $('#end').val();
		queryByTime();
	};
	function changeTime(index){
		var now = new Date();
		index = Number(index);
		if(index != 4){
			switch(index){
			case 0:
				start = getDayStr(new Date(now.getTime()-24*60*60*1000));break;
			case 1:
				start = getDayStr(new Date(now.getTime()-48*60*60*1000));break;
			case 2:
				start = getDayStr(new Date(now.getTime()-7*24*60*60*1000));break;
			case 3:
				start = getLastMonth();break;
			}
			end = getDayStr(now);
		}
		function getLastMonth(){
			var now = new Date();
			var y = now.getFullYear();
			var m = now.getMonth();
			if(m == 0){
				m = 11;
				y --;
			}else{
				m --;
			}
			now.setFullYear(y, m, now.getDate());
			return getDayStr(now);
		}
	}
	function queryByTime(){
		var s = start +' 00:00:00';
		var e = end + ' 23:59:59';
		$('#noIceData').hide();
		$('#iceChart').hide();
		$('#loading').show();
		$.getJSON('/rest/ice/getIceWarningMonitorDialogData/'+params.monitorId+"/"+s+"/"+e,function(result){
			drawCharts(result);
		});
	}
	function drawCharts(data){
		$('#loading').hide();
		if(!data || data.length == 0){
			$('#noIceData').show();
			$('#iceChart').hide();
			iceChart = null;
		}else{
			$('#iceChart').show();
			$('#noIceData').hide();
			iceChart = new CategoryLine('iceChart');
			iceChart.setxAxisTitle("时间").setSubTitle(start+' ~ '+end);
			var colors = [];
			for(var i in data){
				iceChart.setyAxis(data[i].data).setyAxisName(data[i].phase).setyAxisTitle("毫米");
				legends[data[i].phase]=true;
				colors.push(data[i].color);
			}
			iceChart.setDataZoom(true).setDataView(false).setMagicType(false);
			
			var option = iceChart.getOption();
			option.xAxis.min = start+" 00:00:00";
			option.xAxis.max = end+" 23:59:59";
			option.color = colors;
			option.xAxis.type = 'time';
			option.title.subtextStyle = {color:'black'};
			option.tooltip = {
					trigger:'axis',
					formatter:function(params, ticket, callback){
						var str = '';
						var d = new Date(params[0].value[0]);
						var m = getDouble(d.getMonth()+1);
						var day = getDouble(d.getDate());
						var h = getDouble(d.getHours());
						var min = getDouble(d.getMinutes());
						str += '<span>'+m+'-'+day+' '+h+':'+min+'</span><br/>';
						for(var i in data){
							if(legends[data[i].phase]){
								var value = '-';
								for(var j in params){
									if(data[i].phase == params[j].seriesName){
										value = params[j].value[1];
										break;
									}
								}
								str += '<span><span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:'+data[i].color
								+';"></span>'+data[i].phase+'：'+value+'</span><br/>';
							}
						}
						str += '';
						return str;
					}
			};
			option.dataZoom = [{ type:'inside'},
			                   {
				type:'slider',
				dataBackground :{
					lineStyle:{
						color:'blue',
						opacity:1
					}
				}
			                   }];
			
			for(var i in option.series){
				var series = option.series[i];
				var num = 0;
				for(var j in series.data){
					if(series.data[j][1] != "-"){
						num ++;
						if(num > 1){
							break;
						}
					}
				}
				series.showSymbol = num>1?false:true;
				series.label.normal.show = num>1?false:true;
				series.label.emphasis.show = true;
				series.label.emphasis.position = 'top';
				series.areaStyle = {'normal':{'color':'rgba(188,247,241,0.3)'}};
				series.smooth = true;
				series.connectNulls = false;
			}
			iceChart.show();
			iceChart.getChart().on('legendselectchanged',function(params){
				legends = params.selected;
			});
		}
	};
	this.resizeChart = function(){
		if(iceChart){
			iceChart.getChart().resize();
		}
	};
}
function TimeSelector(){
	this.setIndex = function(index){
		var ratios = $('.checkbox-inline');
		if(!isNumber(index) || +index>=ratios.length){
			index = 0;
		}
		$('.checkbox-inline.selected').removeClass('selected');
		$(ratios[index]).addClass('selected');
		if(index == 4){
			this.showQueryDiv();
		}else{
			this.hideQueryDiv();
		}
	};
	this.showQueryDiv = function(){
		$("#queryDiv").css("display","block");
		$("#start").val(start);
		$("#end").val(end);
		$('#start').datetimepicker({
			format : 'yyyy-mm-dd',
			minView : 2,
			autoclose : true,
			endDate : new Date(),
			todayHighlight : true
		});
		$('#end').datetimepicker({
			format : 'yyyy-mm-dd',
			minView : 2,
			autoclose : true,
			endDate : new Date(),
			todayHighlight : true
		});
	};
	this.hideQueryDiv = function(){
		$("#queryDiv").css("display","none");
	};
};

function getDouble(n){
	return n>9?n:"0"+n;
}
function isNumber(obj) {
	obj = Number(obj);
	return obj === +obj;
}
function getDayStr(date){
	var y = date.getFullYear();
	var m = getDouble(date.getMonth()+1);
	var d = getDouble(date.getDate());
	return y+"-"+m+"-"+d;
}
function getRequestParams() {
	var url = location.search; //获取url中"?"符后的字串 
	var theRequest = new Object();
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		for (var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i]
					.split("=")[1]);
		}
	}
	return theRequest;
}
function resizeHtml(){
	con.resizeChart();
}